<?php
/*===================================================
 * Param Replace
 * {link_homepage}
 * {link_logo}
 * {full_name}
 * {user_avatar}
 * {user_email}
 * {action_link}
 * {user_password}
 * {link_privacy}
 * {link_condition}
 *===================================================*/
class Zendvn_Mail{
	
	/*private $_config = array(
		'auth'		=> 'login',
		'ssl' 		=> 'ssl',
		'username'	=> 'labroots@gmail.com',
		'password'	=> 'labroots2012',
		'smtp'		=> 'smtp.gmail.com',
		'port' 		=> '465'
	);*/

	private $_config = array(
						'auth'		=> 'login',
						'ssl' 		=> 'tls',
						'username'	=> 'do_not_reply@labroots.com',
						'password'	=> 'Labdnr2012#',
						'smtp'		=> 'smtp.labroots.com',
						'port' 		=> '25'
					);
	public $_title 	= 'ZendVN';
	public $_from 	= 'do_not_reply@labroots.com';
	public $_to 	= 'anhkhoa.nguyen2610@gmail.com';
	
	public $_linkHome;
	public $_linkPrivacy;
	public $_linkCondition;
	public $_logo;
	
	public function __construct(){
		$cmsUrl = new Zendvn_View_Helper_CmsUrl();
		$this->_linkHome = 'http://' . $_SERVER['HTTP_HOST'] . $cmsUrl->cmsUrl(array(
																		'module' => 'default',
																		'controller' => 'index',
																		'action' => 'index',
																	));
		$this->_linkPrivacy = 'http://' . $_SERVER['HTTP_HOST'] . $cmsUrl->cmsUrl(array(
																		'module' => 'default',
																		'controller' => 'index',
																		'action' => 'privacy',
																	));
		$this->_linkCondition = 'http://' . $_SERVER['HTTP_HOST'] . $cmsUrl->cmsUrl(array(
																		'module' => 'default',
																		'controller' => 'index',
																		'action' => 'conditions',
																	));
		$this->_logo =	'http://' . $_SERVER['HTTP_HOST'] . TEMPLATE_URL . '/public/system/images/logo.png';
	}
	
	public function mailForgotPass($arrParam){
		
		$smtpHost 	= new Zend_Mail_Transport_Smtp ($this->_config['smtp'], $this->_config );
		$user_name 	= $arrParam['firstname'] . ' ' . $arrParam['lastname'];
		$user_name 	= Zendvn_Filter_Filter::filterContent($user_name, 'filter');
		$link_active = $arrParam['link_active'];
		
		/*========================================
		 * Params
		 *========================================*/
		$full_name	= $arrParam['firstname'] . ' ' . $arrParam['lastname'];
		$user_avatar = 'http://' . $_SERVER['HTTP_HOST'] . $arrParam['avatar'];
		$user_email = $arrParam['email'];
		/*========================================
		 * Get Content HTML Email
		 *========================================*/
		$html 		= @file_get_contents(FILE_PATH . '/labroots-files/html-email/forgot_pass.html');	
		$html		= str_replace('{link_homepage}', $this->_linkHome, $html);
		$html		= str_replace('{link_logo}', $this->_logo, $html);
		$html		= str_replace('{full_name}', $full_name, $html);
		$html		= str_replace('{user_avatar}', $user_avatar, $html);
		$html		= str_replace('{user_email}', $user_email, $html);
		$html		= str_replace('{action_link}', $link_active, $html);
		$html		= str_replace('{user_password}', $arrParam['confirm_code'], $html);
		$html		= str_replace('{link_privacy}', $this->_linkPrivacy, $html);
		$html		= str_replace('{link_condition}', $this->_linkCondition, $html);
		
		
		$from		= $this->_from;
		$name		= 'LabRoots';
		$to			= $arrParam['email'];
		$subject	= "Enable new password";
		$mail = new Zend_Mail('UTF-8');
		$mail->setBodyHtml($html);
		$mail->setFrom($from, $name);
		$mail->addTo($to);
		$mail->setSubject($subject);
		$mail->isMultiPart();
		
		
		return $mail->send($smtpHost);
	}
	
	public function mailRegister($arrParam, $option=null){
		$smtpHost 	= new Zend_Mail_Transport_Smtp ($this->_config['smtp'], $this->_config );
		Zend_Mail::setDefaultTransport($smtpHost);
		
		/*========================================
		 * Params
		 *========================================*/
		$full_name	= $arrParam['firstname'] . ' ' . $arrParam['lastname'];
		$user_avatar = 'http://' . $_SERVER['HTTP_HOST'] . $arrParam['avatar'];
		$user_email = $arrParam['email'];
		$link_active = $arrParam['link_active'];
		/*========================================
		 * Get Content HTML Email
		 *========================================*/
		$html 		= @file_get_contents(FILE_PATH . '/labroots-files/html-email/register.html');	
		$html		= str_replace('{link_homepage}', $this->_linkHome, $html);
		$html		= str_replace('{link_logo}', $this->_logo, $html);
		$html		= str_replace('{full_name}', $full_name, $html);
		$html		= str_replace('{user_avatar}', $user_avatar, $html);
		$html		= str_replace('{user_email}', $user_email, $html);
		$html		= str_replace('{action_link}', $link_active, $html);
		$html		= str_replace('{link_privacy}', $this->_linkPrivacy, $html);
		$html		= str_replace('{link_condition}', $this->_linkCondition, $html);
		
		$from		= $this->_from;
		$name		= 'LabRoots';
		$to			= $arrParam['email'];
		$subject	= "Welcome to LabRoots";
		$mail 		= new Zend_Mail('UTF-8');
		$mail->setBodyHtml($html);
		$mail->setFrom($from, $name);
		$mail->addTo($to);
		$mail->setSubject($subject);
		$mail->isMultiPart();
		
		return $mail->send($smtpHost);
	}
	public function mailResume($arrParam, $option=null){
		$smtpHost = new Zend_Mail_Transport_Smtp ($this->_config['smtp'], $this->_config);
		Zend_Mail::setDefaultTransport($smtpHost);
		
		$cmsUrl = new Zendvn_View_Helper_CmsUrl();
		$user_avatar = 'http://' . $_SERVER['HTTP_HOST'] . $arrParam['avatar'];
		$user_email  = $arrParam['from'];
		$linkUser  	 = 'http://' . $_SERVER['HTTP_HOST'] . $cmsUrl->cmsUrl(array(
																		'module' => 'user',
																		'controller' => 'activity',
																		'action' => 'index',
																		'type'=>'other-user',
																		'id'=>$arrParam['apply_id']
																	));
		/*========================================
		 * Get Content HTML Email
		 *========================================*/
		$html 		= @file_get_contents(FILE_PATH . '/labroots-files/html-email/job.html');	
		$html		= str_replace('{link_homepage}', $this->_linkHome, $html);
		$html		= str_replace('{link_logo}', $this->_logo, $html);
		$html		= str_replace('{full_name}', $arrParam['namefrom'], $html);
		$html		= str_replace('{title_job}', $arrParam['title_job'], $html);
		$html		= str_replace('{etitle}', $arrParam['etitle'], $html);
//		$html		= str_replace('{position}', $arrParam['position'], $html);
		$html		= str_replace('{createdby_job}', $arrParam['nameto'], $html);
		$html		= str_replace('{user_avatar}', $user_avatar, $html);
		$html		= str_replace('{user_email}', $user_email, $html);
		$html		= str_replace('{action_link}', $linkUser, $html);
		$html		= str_replace('{link_privacy}', $this->_linkPrivacy, $html);
		$html		= str_replace('{link_condition}', $this->_linkCondition, $html);
		
		$from		= 'do_not_reply@labroots.com';
		$nameForm	= 'LabRoots';
//		$from		= $arrParam['from'];
//		$nameForm	= $arrParam['namefrom'];
//		$to			= $arrParam['receiver'];
		$to			= $arrParam['to'];
		$subject	= $arrParam['name'];
//		$message	= $arrParam['message'];
		$nameTo		= $arrParam['nameto'];
		$type		= $arrParam['type'];
		$filename	= $arrParam['file_attach'];
//		$file 		= @file_get_contents(FILE_PATH.'/resume/'.$filename);
		$file 		= @file_get_contents(FILE_PATH.'/message/'.$filename);
		
		$mail = new Zend_Mail('UTF-8');
		$mail->setBodyHtml($html);
		$mail->setFrom($from, $nameForm);
		$mail->addTo($to, $nameTo);
		$mail->setSubject($subject);
		
		$at = new Zend_Mime_Part($file);
		$at->type        	= $type;
		$at->disposition 	= Zend_Mime::DISPOSITION_ATTACHMENT;
		$at->encoding 	 	= Zend_Mime::ENCODING_BASE64;
		$at->filename    	= $filename;
		$mail->addAttachment($at);
		
		$mail->send($smtpHost);
	}
	
	public function mailFeedback($arrParam, $option=null){
		$smtpHost = new Zend_Mail_Transport_Smtp ($this->_config['smtp'], $this->_config);
		Zend_Mail::setDefaultTransport($smtpHost);
	
		$from		= $arrParam['email'];
		$nameForm	= $arrParam['name'];
		$to			= 'Feedback@LabRoots.com';
		$nameTo		= 'LabRoots';
		$subject	= 'LabRoots Feedback';
		$message	= $arrParam['message'];
	
		$mail = new Zend_Mail('UTF-8');
		$mail->setBodyText($message);
		$mail->setFrom($from, $nameForm);
		$mail->addTo($to, $nameTo);
		$mail->setSubject($subject);
	
		return $mail->send($smtpHost);
	}
	
	public function sendInvite($arrParam, $option=null){
		$smtpHost = new Zend_Mail_Transport_Smtp ($this->_config['smtp'], $this->_config);
		Zend_Mail::setDefaultTransport($smtpHost);
	
		/*========================================
		 * Params
		 *========================================*/
		$full_name	= $arrParam['firstname'] . ' ' . $arrParam['lastname'];
		$user_avatar = 'http://' . $_SERVER['HTTP_HOST'] . $arrParam['avatar'];
		$user_email = $arrParam['email'];
		/*========================================
		 * Get Content HTML Email
		 *========================================*/
		$html 		= @file_get_contents(FILE_PATH . '/labroots-files/html-email/invite.html');	
		$html		= str_replace('{link_homepage}', $this->_linkHome, $html);
		$html		= str_replace('{link_logo}', $this->_logo, $html);
		$html		= str_replace('{full_name}', $full_name, $html);
		$html		= str_replace('{user_avatar}', $user_avatar, $html);
		$html		= str_replace('{user_email}', $user_email, $html);
		$html		= str_replace('{action_link}', $this->_linkHome, $html);
		$html		= str_replace('{link_privacy}', $this->_linkPrivacy, $html);
		$html		= str_replace('{link_condition}', $this->_linkCondition, $html);
		
		
		$from		= $this->_from;;
		$to			= $arrParam['invite_mail'];
		$nameTo		= $arrParam['name_to'];
		$subject	= 'You Are Invited To Join LabRoots';
	
		$mail = new Zend_Mail('UTF-8');
		$mail->setBodyHtml($html);
		$mail->setFrom($from, $full_name);
		$mail->addTo($to, $nameTo);
		$mail->setSubject($subject);
	
		return $mail->send($smtpHost);
	}
	
	public function mailNotification($arrParam, $option=null){
		$smtpHost = new Zend_Mail_Transport_Smtp ($this->_config['smtp'], $this->_config);
		Zend_Mail::setDefaultTransport($smtpHost);
	
		/*========================================
		 * Params
		 * {noti_content}
		 * {noti_link}
		 * {noti_text_link}
		 *========================================*/
		$full_name			= $arrParam['full_name'];
		$user_avatar		= 'http://' . $_SERVER['HTTP_HOST'] . $arrParam['avatar'];		
		$arrParam['link'] 	= 'http://' . $_SERVER['HTTP_HOST'] . $arrParam['link'];
		$user_email 		= $arrParam['email'];
		/*========================================
		 * Get Content HTML Email
		 *========================================*/
		$html 		= @file_get_contents(FILE_PATH . '/labroots-files/html-email/notification.html');	
		$html		= str_replace('{link_homepage}', $this->_linkHome, $html);
		$html		= str_replace('{link_logo}', $this->_logo, $html);
		$html		= str_replace('{link_privacy}', $this->_linkPrivacy, $html);
		$html		= str_replace('{link_condition}', $this->_linkCondition, $html);
		$html		= str_replace('{noti_content}', $arrParam['content'], $html);
		$html		= str_replace('{noti_link}', $arrParam['link'], $html);
		$html		= str_replace('{noti_text_link}', $arrParam['text_link'], $html);
		
		
		$from		= $this->_from;;
		$to			= $user_email;
		$nameTo		= $arrParam['name_to'];
		$subject	= $arrParam['subject'];
	
		$mail = new Zend_Mail('UTF-8');
		$mail->setBodyHtml($html);
		$mail->setFrom($from, $full_name);
		$mail->addTo($to, $nameTo);
		$mail->setSubject($subject);
	
		return $mail->send($smtpHost);
	}
}



















